/**   
 *    
 * 项目名称：SmartKungFuService   
 * 类名称：ClientDiscussAddServlet   
 * 类描述：   用户进行评论接口
 * 创建人：zhanghl   
 * 创建时间：2016年10月10日 上午11:54:34   
 * 修改人：zhanghl   
 * 修改时间：2016年10月10日 上午11:54:34   
 * 修改备注：   
 * @version    
 *    
 */
package com.lst.servlet;

import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.common.JPushEnum;
import com.lst.common.JPushMessage;
import com.lst.entity.request.ClientDiscussReqPara;
import com.lst.entity.response.ClientDiscussResPara2;
import com.lst.model.MstClient;
import com.lst.model.MstDiscuss;
import com.lst.model.MstMessage;
import com.lst.service.MstClientService;
import com.lst.service.MstDiscussService;
import com.lst.service.MstMessageService;
import com.lst.utils.RequestUtils;

/**
 * @ClassName: ClientDiscussAddServlet
 * @Description: 用户进行评论接口
 * @author zhanghl
 * @date 2016年10月10日 上午11:54:34
 *
 */
public class ClientDiscussAddServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;

	private MstDiscussService clientDiscussService = ctx.getBean(MstDiscussService.class); 
	
    private MstMessageService mstMessageService = ctx.getBean(MstMessageService.class);
    
    private MstClientService mstClientService = ctx.getBean(MstClientService.class);

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		Date startDate = new Date();

		ClientDiscussResPara2 resPara = new ClientDiscussResPara2();

		try {
			ClientDiscussReqPara reqPara = getReqPara(request);

			if(isCheckField(reqPara,resPara)){
				
				int clientid = Integer.parseInt(reqPara.getUserid());					
				int tarid = Integer.parseInt(reqPara.getTarid());
				String content = reqPara.getContent();
				int type = Integer.parseInt(reqPara.getType());
			//	String rootid =  reqPara.getRootid();
				int sourceUserId = StringUtils.isEmpty(reqPara.getSourceUserId()) ? 0:Integer.parseInt(reqPara.getSourceUserId());
				
				MstDiscuss md = new MstDiscuss();
				
				md.setCreateuser(clientid);
				md.setTarid(tarid);
				md.setContent(content);
				md.setType(type);
				md.setCreatedate(startDate);
				md.setEnabled(true);
				md.setIsdeleted(false);
				md.setParentid(StringUtils.isEmpty(reqPara.getParentid()) ? null:Integer.valueOf(reqPara.getParentid()));
		//		md.setRootid(StringUtils.isEmpty(rootid) ? null : Integer.valueOf(rootid));
				
				clientDiscussService.insert(md);
				
				if(type==52){
					//对动态评论发送通知
					if(clientid!=sourceUserId){
						MstClient mstClient2=mstClientService.selectByPrimaryKey(Integer.valueOf(sourceUserId));
						if(mstClient2!=null){
							if(mstClient2.getDiscussStatus()){
								MstClient mstClient=mstClientService.selectByPrimaryKey(Integer.valueOf(clientid));
								String name=mstClient.getNickname();
								//String portrait=mstClient.getReserved11();
								
							      MstMessage message=new MstMessage();
							      message.setReceiveid(sourceUserId);
							      message.setReceivetype(CommCode.SYSCODE_MSG_TYPE_RECEIVE_CLIENT);
							      message.setSendid(clientid);
							      message.setSendtype(CommCode.SYSCODE_MSG_TYPE_SEND_CLIENT);
							      message.setContent("评论了你的动态");
							    //  message.setReserved12(portrait);
							      message.setEnabled(true);
							      message.setIsdeleted(false);
							      message.setCreateuser(-1);
							      message.setCreatedate(startDate);
							      message.setReserved1(tarid);
							      message.setReserved2(52);
							      message.setReserved7(false);
							      message.setType(CommCode.SYSCODE_MSG_TYPE_PUSH);
							      mstMessageService.insert(message);
							      message.setReserved12(name);
							      JPushMessage.pushMessage(message, JPushEnum.ALL_ALIAS);
							}
						}
					}
					
					
				}
				if(type==50){
					//对回答评论发送通知
					if(clientid!=sourceUserId){
						MstClient mstClient2=mstClientService.selectByPrimaryKey(Integer.valueOf(sourceUserId));
						if(mstClient2!=null){
							if(mstClient2.getDiscussStatus()){
								MstClient mstClient=mstClientService.selectByPrimaryKey(Integer.valueOf(clientid));
								String name=mstClient.getNickname();
								//String portrait=mstClient.getReserved11();
								
							      MstMessage message=new MstMessage();
							      message.setReceiveid(sourceUserId);
							      message.setReceivetype(CommCode.SYSCODE_MSG_TYPE_RECEIVE_CLIENT);
							      message.setSendid(clientid);
							      message.setSendtype(CommCode.SYSCODE_MSG_TYPE_SEND_CLIENT);
							      message.setContent("评论了你的回答");
							    //  message.setReserved12(portrait);
							      message.setEnabled(true);
							      message.setIsdeleted(false);
							      message.setCreateuser(-1);
							      message.setCreatedate(startDate);
							      message.setReserved1(tarid);
							      message.setReserved2(50);
							      message.setReserved7(false);
							      message.setType(CommCode.SYSCODE_MSG_TYPE_PUSH);
							      mstMessageService.insert(message);
							      message.setReserved12(name);
							      JPushMessage.pushMessage(message, JPushEnum.ALL_ALIAS);
							}
						}
					}
					
				}
				//若为一级评论存放root值
//				if(StringUtils.isEmpty(reqPara.getParentid())){
//					md.setRootid(md.getId());
//					clientDiscussService.updateByPrimaryKeySelective(md);
//				}
                resPara.setDiscussId(md.getId());
				resPara.setCode(CommCode.M_Y000000);
				resPara.setMessage(CommCode.M_Y000001);
			}

		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}

		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
		//	logger.info("ClientDiscussServlet jsonResult:" + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

		//	logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public ClientDiscussReqPara getReqPara(HttpServletRequest request) {

		ClientDiscussReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new ClientDiscussReqPara());

			reqPara = (ClientDiscussReqPara) baseRequest.clone();

			String parentid = request.getParameter("parentid");
			String userid = request.getParameter("userid");
			String tarid = request.getParameter("tarid");
			String content = request.getParameter("content");
			String type = request.getParameter("type");
		//	String rootid = request.getParameter("rootid");
			String sourceUserId = request.getParameter("sourceUserId");

			logger.info("reqPara parentid: " + parentid);
			logger.info("reqPara userid: " + userid);
			logger.info("reqPara tarid: " + tarid);
			logger.info("reqPara content:" + content);
			logger.info("reqPara type: " + type);
		//	logger.info("reqPara rootid: " + rootid);
			logger.info("reqPara sourceUserId: " + sourceUserId);

			reqPara.setParentid(parentid);
			reqPara.setUserid(userid);
			reqPara.setTarid(tarid);
			reqPara.setContent(content);
			reqPara.setType(type);
		//	reqPara.setRootid(rootid);
            reqPara.setSourceUserId(sourceUserId);
		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		ClientDiscussReqPara cdReqPara = (ClientDiscussReqPara) reqPara;
		BaseResponse cdResPara = (BaseResponse) resPara;

		if (StringUtils.isEmpty(cdReqPara.getUserid())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000002);
		} else if (StringUtils.isEmpty(cdReqPara.getTarid())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_B000001);
		} else if (StringUtils.isEmpty(cdReqPara.getType())){
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_B000003);
		} else if (StringUtils.isEmpty(cdReqPara.getContent())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_B000002);
		} else {
			flag = true;
		}

		return  flag;
	}
}
